<?php
/**
 * @Author: Administrator
 * @Date:   2014-08-03 22:42:38
 * @Last Modified by:   Administrator
 * @Last Modified time: 2014-08-25 21:40:01
 */

/**
* 职位查询视图
*/
class ResumeInfoViewModel extends ViewModel
{
	public $table = 'people_resume1';
	public $view = array(
				'people_resume2'=>array(
					'type'=>LEFT_JOIN,
					'on'=>'people_resume1.reid=people_resume2.resume1_reid'
				),
				'people_resume3'=>array(
					'type'=>LEFT_JOIN,
					'on'=>'people_resume1.reid=people_resume3.resume1_reid'
				),
				'people_resume7'=>array(
					'type'=>LEFT_JOIN,
					'on'=>'people_resume1.reid=people_resume7.resume1_reid'
				),
				'relation_resume2_pos' =>array(
					'type'=>LEFT_JOIN,
					'on'=>'people_resume1.reid = relation_resume2_pos.re_reid'
					)
			);
	public $where = array();

	public function __init()
	{
		$this->where=array(
			'publicstate'		=>	1,
			'verifystate'	=>	2,
			C('DB_PREFIX').'people_resume1.endtime'		=>	array('gt'=>time())
		);
	}
	public function get_all()
	{
		// p($data);
		$where = $this->where;

		$city_one = Q('get.city_one',0,'intval');
		$city_second = Q('get.city_second',0,'intval');

		$cityModel = K('City');
		#如果是二级城市直接返回
		if ($city_second) {
			$city_cityid = $city_second;
		}else{
			#如果$city_one 不等于0
			if ($city_one) {
				#如果是一级城市，返回所有的一级城市里的内容以及该城市的子孙城市内容
				$city_cityid = $cityModel->get_city_id($city_one);
			}else{#等于0的情况
				$city_cityid = array_keys($cityModel->city);
			}
		}


		#职位处理逻辑和city的一样
		$pos_one = Q('get.pos_one',0,'intval');
		$pos_second = Q('get.pos_second',0,'intval');
		$posModel = K('PosCate');
		if ($pos_second) {
			$pos_id = $pos_second;
		}else{
			if ($pos_one) {
				$pos_id = $posModel->get_pos_id($pos_one);
			}else{
				$pos_id = array_keys($posModel->pos);
			}
		}

		if ($keyword = Q('get.keyword')) $where[] = 'resumename like "%'.$keyword.'%"';
		else unset($_GET['keyword']);
		if ($edu = Q('get.edu')) $where['edu'] = $edu;
		else unset($_GET['edu']);
		if ($workyear = Q('get.workyear')) $where['workyear'] = $workyear;
		else unset($_GET['workyear']);
		if ($sex = Q('get.sex')) $where['sex'] = $sex;
		else unset($_GET['sex']);	
		if(isset($city_cityid))$where[C('DB_PREFIX') . 'people_resume2.city_cityid'] = $city_cityid;
		if (isset($pos_id))$where[C('DB_PREFIX') . 'relation_resume2_pos.re_posid'] = $pos_id;
		$total = $this->where($where)->count();
		$page = new Page($total);
		$limit = $page->limit();
		$temp = $this->join(array('people_resume2','relation_resume2_pos','people_resume3'))->where($where)->findAll($limit);
		
		foreach ($temp as $k=>$v) {
			unset($temp[$k]['re_posid']);
		}
		// p($temp);
		$temp = unique_arr($temp,'_',true);
		// p($temp);
		$temp['page']=$page->show();
		return $temp;
	}

	#前台首页显示最新职位
	public function get_data($field=null,$limit=null)
	{
		$newResume = S('newResume',false,0,array('dir'=>CACHE_PATH));
		if(!$newResume){
			$order = 'sendtime DESC,freshtime DESC';
			$newResume = $this->field($field)->join(array('people_resume2','people_resume3','people_resume7'))->where($this->where)->order($order)->limit()->all();
			$newResume = K('ResumePosView')->get_pos_index($newResume);
			// p($newResume);exit;
			// S('newResume',$newResume,30,array('dir'=>CACHE_PATH));
		}

		// p($newResume);
		return $newResume;
	}

	public function get_face_data($field=null,$limit=null)
	{
		$order = 'sendtime DESC,freshtime DESC';
		$newFaceResume = $this->field($field)->join(array('people_resume2','people_resume3','people_resume7'))->where($this->where)->order($order)->limit()->all();
		// p($newFaceResume);
		foreach ($newFaceResume as $key => $value) {
			if(!$value['face']){
				unset($newFaceResume[$key]);
			}
		}
		return $newFaceResume;
	}


	public function get_hot_data($field=null,$limit=null)
	{
		$where = $this->where;
		$order = 'click DESC,sendtime DESC,freshtime DESC';
		$data = $this->field($field)->join(array('people_resume2','people_resume3','people_resume7'))->where($this->where)->order($order)->limit()->all();
		// p($data);
		return $data;
	}
}